/**
* @rili    带农历和节日的日历
* @charset UTF-8
* @Author  hy(ymz316@126.com)
* @blog    www.hollowman.cn 
* @Time    2020-03
* @Version 1.0.0

**/

var rili = {
    selectedDate:new Date(),
    calendar:function(domid) {//月历控制
        //获取当前日期
        var nowDate=new Date();
        y=nowDate.getFullYear();
        m=nowDate.getMonth()+1;
        d=nowDate.getDate();
        var objdom=document.getElementById(domid);//获取月历需要放入的html标签对象

        //******************控制菜单显示部分 开始**************************//
        var calendaropt=document.createElement("div");
        calendaropt.className="form-inline";
        objdom.appendChild(calendaropt);       
        //前一月按钮
        var prectrl=document.createElement("label");
        prectrl.className='btn btn-link';
        prectrl.innerHTML='<i class="fa fa-backward"></i>';     
        calendaropt.appendChild(prectrl);  
        //年份选择器
        var yearsel=document.createElement("select");
        yearsel.className="form-control-select input-sm";
        calendaropt.appendChild(yearsel);
        var yearid=new Array();      
        for(i=1900;i<=2050;i++){            
            yearid[i]=document.createElement("option");
            yearid[i].id="year"+i;
            yearid[i].date=i;
            yearid[i].text=yearid[i].date+'年';
            yearsel.appendChild(yearid[i]);
            if(i==y){
                yearid[i].selected=true;
            }
        }
        //月份选择器
        var monthsel=document.createElement("select");
        monthsel.className="form-control-select input-sm";
        calendaropt.appendChild(monthsel);
        var monthid=new Array();      
        for(i=0;i<12;i++){            
            monthid[i]=document.createElement("option");
            monthid[i].id="month"+(i+1);
            monthid[i].date=i+1;
            monthid[i].text=monthid[i].date+'月';
            monthsel.appendChild(monthid[i]);
            if(m==i+1){
                monthid[i].selected=true;
            }
        }
        //后一月按钮
        var nextctrl=document.createElement("label");
        nextctrl.className='btn btn-link';
        nextctrl.innerHTML='<i class="fa fa-forward"></i>';  
        calendaropt.appendChild(nextctrl);  

        //返回今日按钮
        var nowctrl=document.createElement("label");
        nowctrl.innerHTML=' <span class="rounded bg-primary text-light" style="padding:0px 3px 0px 3px">今</span>' ;
        nowctrl.className='btn btn-link';
        calendaropt.appendChild(nowctrl);  
        //******************控制菜单显示部分 结束**************************//

        //******************月历载入 开始**************************//
        //月历div
        var objcalendar=document.createElement("div");
        objcalendar.className="table-responsive";
        objcalendar.innerHTML=rili.yueli(y,m); 
        objdom.appendChild(objcalendar);  

        //选中日信息显示div
        var objdayinfo=document.createElement("div");
        objdayinfo.id='dayinfo';
        objdom.appendChild(objdayinfo);
        //******************月历载入 结束**************************//
        
        rili.selectDate();    //选择日期  

        //******************控制菜单事件处理 开始**************************//
        yearsel.onchange=function(){
            objcalendar.innerHTML=rili.yueli(yearsel.options[yearsel.selectedIndex].date,monthsel.options[monthsel.selectedIndex].date); 
            //切换日期
            rili.selectedDate=rili.changeselectedDate(yearsel.options[yearsel.selectedIndex].date,monthsel.options[monthsel.selectedIndex].date,rili.selectedDate.getDate());
            rili.selectDate();//选择日期
        }
        monthsel.onchange=function(){
            objcalendar.innerHTML=rili.yueli(yearsel.options[yearsel.selectedIndex].date,monthsel.options[monthsel.selectedIndex].date); 
            //切换日期
            rili.selectedDate=rili.changeselectedDate(yearsel.options[yearsel.selectedIndex].date,monthsel.options[monthsel.selectedIndex].date,rili.selectedDate.getDate());
            rili.selectDate();//选择日期
        }

        prectrl.onclick=function(){
            var currentDate=new Date(yearsel.options[yearsel.selectedIndex].date,monthsel.options[monthsel.selectedIndex].date-1,1);            
            currentDate = new Date(currentDate.setDate(currentDate.getDate() - currentDate.getDate() - 1));
            currentDate = new Date(currentDate);        
            document.getElementById("year"+currentDate.getFullYear()).selected=true;
            document.getElementById("month"+(currentDate.getMonth()+1)).selected=true;     
            objcalendar.innerHTML=rili.yueli(yearsel.options[yearsel.selectedIndex].date,monthsel.options[monthsel.selectedIndex].date);    
            //切换日期
            rili.selectedDate=rili.changeselectedDate(yearsel.options[yearsel.selectedIndex].date,monthsel.options[monthsel.selectedIndex].date,rili.selectedDate.getDate());
            rili.selectDate();//选择日期
        }

        nextctrl.onclick=function(){
            var currentDate=new Date(yearsel.options[yearsel.selectedIndex].date,monthsel.options[monthsel.selectedIndex].date-1,1);            
            currentDate = new Date(currentDate.setDate(currentDate.getDate() + 31))
            currentDate = new Date(currentDate);       
            document.getElementById("year"+currentDate.getFullYear()).selected=true;
            document.getElementById("month"+(currentDate.getMonth()+1)).selected=true;   
            objcalendar.innerHTML=rili.yueli(yearsel.options[yearsel.selectedIndex].date,monthsel.options[monthsel.selectedIndex].date);    
            //切换日期
            rili.selectedDate=rili.changeselectedDate(yearsel.options[yearsel.selectedIndex].date,monthsel.options[monthsel.selectedIndex].date,rili.selectedDate.getDate());
            rili.selectDate();//选择日期
        }

        nowctrl.onclick=function(){
            currentDate = new Date(); 
            rili.selectedDate=currentDate;      
            document.getElementById("year"+currentDate.getFullYear()).selected=true;
            document.getElementById("month"+(currentDate.getMonth()+1)).selected=true; 
            objcalendar.innerHTML=rili.yueli(yearsel.options[yearsel.selectedIndex].date,monthsel.options[monthsel.selectedIndex].date);    
            rili.selectDate();
        }
        //******************控制菜单事件处理 结束**************************//
    },
    yueli: function (y,m) {//月历填充
        var nowDate = new Date();//获取当前日期
        var innerDivHtmlStr = '<table class="table text-nowrap table-hover"><thead><tr><th>一</th><th>二</th><th>三</th><th>四</th><th>五</th><th style="color:#f30">六</th><th style="color:#f30">日</th></tr></thead><tbody>';
        var d = this.mBeginDay(y, m);//获取月历第一天
        var k=Math.ceil((new Date(y,m,1).getTime()-d.getTime())/(7*24*3600*1000));//获取显示的星期数
 for (i = 0; i < k; i++) {
            innerDivHtmlStr = innerDivHtmlStr + '<tr>';
            for (j = 0; j < 7; j++) {
                if (d.getFullYear() == nowDate.getFullYear() && d.getMonth() == nowDate.getMonth() && d.getDate() == nowDate.getDate()) {
                    innerDivHtmlStr = innerDivHtmlStr + '<td id="'+this.datetostr(d.getFullYear(),d.getMonth()+1,d.getDate()) +'"  date="'+this.datetostr(d.getFullYear(),d.getMonth()+1,d.getDate()) +'" class="calendartd"><div id="divtoday"><div style="text-align:center"><b style="color:red">' + d.getDate() + '</b></div><div style="font-size:10px;text-align:center;color:red">' + this.dLunar(d.getFullYear(), d.getMonth() + 1, d.getDate()) + '</div></div></td>';
                } else {
                    innerDivHtmlStr = innerDivHtmlStr + '<td id="'+this.datetostr(d.getFullYear(),d.getMonth()+1,d.getDate()) +'"  date="'+this.datetostr(d.getFullYear(),d.getMonth()+1,d.getDate()) +'" class="calendartd"><div><div style="text-align:center">' + d.getDate() + '</div><div style="font-size:10px;text-align:center;color:brown">' + this.dLunar(d.getFullYear(), d.getMonth() + 1, d.getDate()) + '</div></div></td>';
                }
                d = new Date(d.setDate(d.getDate() + 1));
                d = new Date(d);
            }
            innerDivHtmlStr = innerDivHtmlStr + '</tr>';
        }
        innerDivHtmlStr = innerDivHtmlStr + '</tbody></table>';
        return innerDivHtmlStr;
    },

    datetostr:function(y,m,d){//日期转字符串
        if(m<10){
            m = '0' + m;
        }
        if(d<10){
            d = '0' + d;
        }
		return y + '-' + m + '-' + d;
    },
    strtodate:function(str){
        var dateOut=[];
        var dateArr=str.split('-');
        dateOut.push(parseInt(dateArr[0]));
        dateOut.push(parseInt(dateArr[1]));
        dateOut.push(parseInt(dateArr[2]));
        return dateOut;
    },

    //判断某个月的月历中的第一天为几号
    mBeginDay: function (y, m) {
        var oneDay = new Date(y, m - 1, 1);//Date()函数中，月份是0-11
        var weekday = oneDay.getDay();//0 1 2 3 4 5 6 
        if (weekday > 0) {
            oneDay = new Date(oneDay.setDate(oneDay.getDate() - weekday + 1));
            oneDay = new Date(oneDay);
        } else {
            oneDay = new Date(oneDay.setDate(oneDay.getDate() - 6));
            oneDay = new Date(oneDay);
        }
        return oneDay;
    },
    //切换选取日期
    changeselectedDate:function(y,m,d){
        var monthDays=new Date(y,m,0).getDate();//获取当前第m月有多少天
        if(d>monthDays){
            d=monthDays;
        }
        return new Date(y,m-1,d);
    },

    //日期选择
    selectDate:function(){
        var objdayinfo=document.getElementById('dayinfo');
        var objtd=document.getElementsByClassName("calendartd");
        //未点击时读取selectdeDate日期
        var selecteddate=rili.datetostr(rili.selectedDate.getFullYear(),rili.selectedDate.getMonth()+1,rili.selectedDate.getDate());    
        rili.doSelectedDate(selecteddate);                    
        objdayinfo.innerHTML=rili.selectDateInfo(selecteddate);
        //找到点击的对象并执行选中日操作事件
        if(objtd.length>0){
            for(var i=0;i<objtd.length;i++){   
                objtd[i].onclick = function(){
                    var selecteddate=this.getAttribute('date');
                    rili.selectedDate=new Date(selecteddate);//selectedDate设置成选定的日期
                    rili.doSelectedDate(selecteddate);                    
                    objdayinfo.innerHTML=rili.selectDateInfo(selecteddate);
                    
                }
            }
        }
    },
    doSelectedDate:function(str){//对选中日期操作
        var objtdtemp=document.getElementsByClassName("calendartd");
        for(var i=0;i<objtdtemp.length;i++){            
            if(objtdtemp[i].getAttribute('date')==str){
                objtdtemp[i].firstElementChild.style.border='2px solid blue'; 
            }else{
                objtdtemp[i].firstElementChild.style.border='2px solid transparent';  
            }
        }
    },
    selectDateInfo:function(str){
        var selectDate=this.strtodate(str);
        var dayLunar = calendar.solar2lunar(selectDate[0], selectDate[1], selectDate[2]);
        var ncWeek=dayLunar.ncWeek;
        var lunarFull=dayLunar.IMonthCn+dayLunar.IDayCn;
        var gzYear=dayLunar.gzYear+'年';//干支年
        var sxYear='【'+dayLunar.Animal+'】年';//生肖年
        var gzMonth=dayLunar.gzMonth+'月';
        var gzDay=dayLunar.gzDay+'日';
        var str=str+'&nbsp&nbsp'+ncWeek+'&nbsp&nbsp'+lunarFull+'&nbsp&nbsp'+gzYear+'&nbsp&nbsp'+sxYear+'&nbsp&nbsp'+gzMonth+'&nbsp&nbsp'+gzDay;
        return str; 

    },
    dLunar: function (y, m, d) {//农历带月份，节气
        var dayLunar = calendar.solar2lunar(y, m, d);
        if (dayLunar.lDay == 1) {//每月初一用月份表示
            return dayLunar.IMonthCn;
        } else if (dayLunar.isTerm) {//如果属于24节气，则显示节气
            return dayLunar.Term;
        } else {
            return dayLunar.IDayCn;
        }
    }

};



